﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Menu : MonoBehaviour
{
    [Header("组件")]
    public Animator listAnimator;
    public GameObject Ui;
    public GameObject list;
    public Animator uiAnimator;
    public GameObject camera;
    public GameObject Player;
    
    Vector3 offset;
    float time;
    bool start = false;
    bool cameracheck = false;



    bool Action;
    public Transform target;
    private Vector3 playerOffset;
    void Start()
    {
       time = 1f;
        offset = Player.transform.position - camera.transform.position;
        playerOffset = target.position - this.transform.position;
    }

    private void Update()
    {
        keyCheck();
        if (cameracheck == false) 
        {
            cameraMove();
        }
        if (Action)
        {
            this.transform.position = target.position - offset;
        }
    }
    //void Fallow()
    //{
    //    if (targetTrans != null)
    //    {
    //        Vector3 pos = Camera.main.WorldToViewportPoint(targetTrans.position);

    //        Vector3 offset = targetTrans.position - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, pos.z));

    //        Vector3 destination = transform.position + offset;

    //        Vector3 cameraPos = transform.position;

    //        if (destination.y > transform.position.y)
    //        {
    //            cameraPos.y = Mathf.Lerp(transform.position.y, destination.y, lerpTime);
    //        }
    //        transform.position = cameraPos;

    //    }
    //}

    public void StartGame()
    {
        uiAnimator.SetBool("isUsing", false);
        Time.timeScale = 1f;
        if (time == 1f) 
        {
            start = true;
            time--;
        }
        Invoke("LoadScene",2.5f);
    }
    void LoadScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    public void cameraMove()
    {
        if (start)
        {
            if (Mathf.Abs(Player.transform.position.x -camera.transform.position.x) <0.1f && Mathf.Abs(Player.transform.position.y - camera.transform.position.y) <0.1f)
            {   
                cameracheck = true;
                Action = true;
            }
            else
            {
                camera.transform.position = Vector3.Lerp(camera.transform.position,new Vector3(Player.transform.position.x,Player.transform.position.y,-10f),0.02f);
            }
        }
    }

    public void List() 
    {
        Time.timeScale = 1f;
        list.SetActive(true);
    }

    public void Unuse() 
    {
        listAnimator.SetBool("opening", false);
    }

    public void Exit() 
    {
        Application.Quit();
    }


    public void keyCheck() 
    {
        if (Input.GetKey(KeyCode.Escape) && time ==0f) 
        {
            uiAnimator.SetBool("isUsing", true);
        }
    }
    public void Pause()
    {
        Time.timeScale = 0;
    }
    public void Continue()
    {
        Time.timeScale = 1;
    }
}
